package com.decheng.mq.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.decheng.mq.common.api.CommonResult;
import com.decheng.mq.sender.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 孔德成
 * @slogan 致敬大师，致敬未来的你
 * @date 2021/9/16 2:31 下午
 * @desc
 */
@Api(tags = "RabbitMQ功能测试")
@RestController
@RequestMapping("/rabbit")
public class RabbitController {

    @Autowired
    private SimpleSender simpleSender;

    @Autowired
    private WorkSender workSender;

    @Autowired
    private FanoutSender fanoutSender;

    @Autowired
    private DirectSender directSender;

    @Autowired
    private TopicSender topicSender;

    @ApiOperation(value = "简单模式", notes = "每隔一秒钟发送一条 'Hello,World' 到 'simple.hello' 队列中, 共发送10条")
    @RequestMapping(value = "/simple", method = RequestMethod.GET)
    public CommonResult simpleTest() {
        for (int i = 0; i < 10; i++) {
            simpleSender.send();
            ThreadUtil.sleep(1000);
        }
        return CommonResult.success(null);
    }


    @ApiOperation(value = "工作模式", notes = "每隔一秒钟向队列 'work.hello' 中发送消息，消息中包含一定数量的 '.' 号,发送 10 次")
    @RequestMapping(value = "/work", method = RequestMethod.GET)
    public CommonResult workTest() {
        for (int i = 0; i < 10; i++) {
            workSender.send(i);
            ThreadUtil.sleep(1000);
        }
        return CommonResult.success(null);
    }

    @ApiOperation(value = "发布/订阅模式", notes = "向 'exchange.fanout' 交换机发送耗时任务")
    @RequestMapping(value = "/fanout", method = RequestMethod.GET)
    public CommonResult fanoutTest() {
        for (int i = 0; i < 10; i++) {
            fanoutSender.send(i);
            ThreadUtil.sleep(1000);
        }
        return CommonResult.success(null);
    }

    @ApiOperation("路由模式")
    @RequestMapping(value = "/direct", method = RequestMethod.GET)
    public CommonResult directTest() {
        for (int i = 0; i < 10; i++) {
            directSender.send(i);
            ThreadUtil.sleep(1000);
        }
        return CommonResult.success(null);
    }

    @ApiOperation("通配符模式")
    @RequestMapping(value = "/topic", method = RequestMethod.GET)
    public CommonResult topicTest() {
        for (int i = 0; i < 10; i++) {
            topicSender.send(i);
            ThreadUtil.sleep(1000);
        }
        return CommonResult.success(null);
    }
}
